package com.jyb.taglib.pager;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.Tag;
import javax.servlet.jsp.tagext.TagSupport;

public class PagerAmazeUiTag extends TagSupport{

	private static final long serialVersionUID = -8601322343424552683L;
	private static int maxShowNumber=9;
	
	private Integer pageNumber;
	
	private Integer pageSize;
	
	private Integer totalPage;

	private Integer totalRow;
	
	private String formId;

	public Integer getPageNumber() {
		return pageNumber;
	}

	public void setPageNumber(Integer pageNumber) {
		this.pageNumber = pageNumber;
	}

	public Integer getPageSize() {
		return pageSize;
	}

	public void setPageSize(Integer pageSize) {
		this.pageSize = pageSize;
	}

	public Integer getTotalPage() {
		return totalPage;
	}

	public void setTotalPage(Integer totalPage) {
		this.totalPage = totalPage;
	}

	public Integer getTotalRow() {
		return totalRow;
	}

	public void setTotalRow(Integer totalRow) {
		this.totalRow = totalRow;
	}

	public String getFormId() {
		return formId;
	}

	public void setFormId(String formId) {
		this.formId = formId;
	}

	public int doEndTag() throws JspException {
		JspWriter out = pageContext.getOut();// 得到out
		
		try {
			
			if(formId!=null){
				formId=formId.trim();
			}else{
				formId="searchForm";
			}
			
			StringBuffer pager=new StringBuffer("");
			
			if(totalPage!=null){
				if(totalPage==0){
					pager.append("<p class=\"text-info\">暂无数据</p>");
					//pager.append("<button type=\"button\" class=\"btn btn-default\">暂无数据</button>");
				}else{
					pager.append("<nav><ul class=\"pagination\">");
					
					if(pageNumber<=1){
						pager.append("<li class='disabled'><a href=\"#\" aria-label=\"Previous\"><span aria-hidden=\"true\">&laquo;</span></a></li>");
					}else{
						pager.append("<li><a href=\"javascript:form.toPage('"+formId+"',"+(pageNumber-1)+","+pageSize+");\" aria-label=\"Previous\"><span aria-hidden=\"true\">&laquo;</span></a></li>");
					}
					
					if(totalPage<=maxShowNumber){
						for(int i=1;i<=totalPage;i++){
							pager.append("<li class='"+(i==pageNumber?"active":"")+"'><a href=\"javascript:form.toPage('"+formId+"',"+i+","+pageSize+");\">"+i+"</a></li>");
						}
					}else{
						int indexPage=pageNumber-(maxShowNumber/2);
						for(int i=0;i<maxShowNumber;i++){
							if((indexPage+i)>0&&(indexPage+i)<=totalPage){
								pager.append("<li class='"+((indexPage+i)==pageNumber?"active":"")+"'><a href=\"javascript:form.toPage('"+formId+"',"+(indexPage+i)+","+pageSize+");\">"+(indexPage+i)+"</a></li>");
							}
						}
					}
					
					if(pageNumber>=totalPage){
						pager.append("<li class='disabled'><a href=\"#\" aria-label=\"Next\"><span aria-hidden=\"true\">&raquo;</span></a></li>");
					}else{
						pager.append("<li><a href=\"javascript:form.toPage('"+formId+"',"+(pageNumber+1)+","+pageSize+");\" aria-label=\"Next\"><span aria-hidden=\"true\">&raquo;</span></a></li>");
					}
					
					pager.append("<li class='disabled'><a href='#'>共"+totalRow+"条数据</a></li>");
					pager.append("</ul></nav>");
				}
			}else{
				pager.append("<p class=\"text-info\">暂无数据</p>");
			}
			
			
			
			out.print(pager.toString());
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		return Tag.EVAL_PAGE;
	}
	
}
